import 'package:flutter/material.dart';
import '../model/health_record.dart';
import '../service/database_service.dart';
import 'base_record_dialog.dart';

class AddCustomRecordDialog extends BaseRecordDialog {
  const AddCustomRecordDialog({Key? key, required String babyId})
    : super(key: key, babyId: babyId);

  @override
  _AddCustomRecordDialogState createState() => _AddCustomRecordDialogState();
}

class _AddCustomRecordDialogState
    extends BaseRecordDialogState<AddCustomRecordDialog> {
  final _titleController = TextEditingController();
  final _contentController = TextEditingController();

  @override
  void dispose() {
    _titleController.dispose();
    _contentController.dispose();
    super.dispose();
  }

  @override
  String getDialogTitle() => '记录自定义事件';

  @override
  Widget buildCustomContent(BuildContext context) {
    return Column(
      children: [
        TextField(
          controller: _titleController,
          decoration: InputDecoration(
            labelText: '事件标题',
            border: OutlineInputBorder(),
          ),
        ),
        SizedBox(height: 16),
        TextField(
          controller: _contentController,
          decoration: InputDecoration(
            labelText: '事件内容',
            border: OutlineInputBorder(),
          ),
          maxLines: 3,
        ),
      ],
    );
  }

  @override
  Future<void> saveRecord(BuildContext context) async {
    if (_titleController.text.isEmpty) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(SnackBar(content: Text('请输入事件标题')));
      return;
    }

    try {
      await DatabaseService().addHealthRecord(
        HealthRecord(
          babyId: widget.babyId,
          type: RecordType.custom,
          timestamp: selectedDateTime,
          data: {
            'title': _titleController.text,
            'content': _contentController.text,
            'notes': notes,
          },
        ),
      );
      Navigator.pop(context);
    } catch (e) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(SnackBar(content: Text('保存失败: $e')));
    }
  }
}
